#include "blockqueue.hpp"
int i=0;
int re=0;
pthread_mutex_t lock =PTHREAD_MUTEX_INITIALIZER;
void* product(void* arg){
    block_queue<int>* que =( block_queue <int>*)arg;
    while(true){
    sleep(1);
    que->push(i);
    std::cout<<"product i:"<<i<<std::endl;
    pthread_mutex_lock(&lock);
    i++;
    if(i==13){
    i=0;
    }
    pthread_mutex_unlock(&lock);
    } 
}
void* consume(void* arg){
      block_queue<int>* que =( block_queue <int>*)arg;
    int j=0;
     while(true){
    sleep(1);
    que->pop(&j);
    std::cout<<"consume i:"<<j<<std::endl;
    }
}
int main(){
    block_queue <int> ring_que(12);
    pthread_t tid_1;
    pthread_t tid_2;
    pthread_t tid_3;
    pthread_t tid_4;
    pthread_create(&tid_1,nullptr,product,(void*)(&ring_que));
    pthread_create(&tid_2,nullptr,consume,(void*)(&ring_que));
    pthread_create(&tid_3,nullptr,product,(void*)(&ring_que));
    pthread_create(&tid_4,nullptr,consume,(void*)(&ring_que));
    std::cout<<"线程创建完毕"<<std::endl;
    while(true){

    }
    pthread_join(tid_1,nullptr);
    pthread_join(tid_2,nullptr);
    pthread_join(tid_3,nullptr);
    pthread_join(tid_4,nullptr);
}